#!/bin/bash

echo "Starting Snake Game with GPU acceleration..."

# Set JVM parameters for GPU optimization
export JAVA_OPTS="-Xmx8g -Xms2g"
export JAVA_OPTS="$JAVA_OPTS -Dorg.bytedeco.javacpp.maxbytes=8G"
export JAVA_OPTS="$JAVA_OPTS -Dorg.bytedeco.javacpp.maxphysicalbytes=12G"
export JAVA_OPTS="$JAVA_OPTS -Dorg.nd4j.linalg.factory=org.nd4j.linalg.gpu.CudaNDArrayFactory"
export JAVA_OPTS="$JAVA_OPTS -Dorg.nd4j.linalg.ops=org.nd4j.linalg.gpu.ops.CudaOpExecutioner"

echo "JVM Options: $JAVA_OPTS"
echo

# Check if CUDA is available
if command -v nvidia-smi &> /dev/null; then
    echo "GPU Status:"
    nvidia-smi --query-gpu=name,memory.total,memory.used --format=csv,noheader,nounits
    echo
else
    echo "WARNING: nvidia-smi not found. GPU may not be available."
    echo
fi

# Run the application with GPU support
echo "Running with GPU acceleration..."
java $JAVA_OPTS -cp "target/classes:target/dependency/*" org.dxy.trigger.SnakeGameWithDQN

# Alternative: use Maven exec (slower but handles dependencies automatically)
# mvn exec:java -Dexec.mainClass="org.dxy.trigger.SnakeGameWithDQN"